package cz.spock.core.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import cz.spock.core.entity.Person;
import cz.spock.exception.InvalidInputException;

/**
 * 
 * @author miso
 *
 */
public class UserService extends BaseService {

    /**
     * receives user's values
     * validates values
     * creates and persists user (person)
     * returns person object with assigned id  
     * 
     * @param login
     * @param email
     * @return
     */
    public Person createPerson(String name, String surname, String birthday, String gender) {
        // validate data
        if(name == null) {
            log.fine("name is null");            
            //throw new InvalidInputException("Login is required.", 411);
        }
        if(surname == null) {
            log.fine("surname is null");            
            //throw new InvalidInputException("Email is required.", 412);
        }
        //check gender
        if(gender != null) {
            if(gender.equals("male") || gender.equals("man")) {
                gender = "male";
            } else if(gender.equals("female") || gender.equals("woman")) {
                gender = "female";
            } else gender = null;
        }
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        // create Person
        Person person = new Person();
        person.setName(name);
        person.setSurname(surname);
        person.setGender(gender);
        try {
            person.setBirthday(df.parse(birthday));
        } catch (ParseException e) {
            throw new InvalidInputException("Birthday has invalid format.", 410);
        }
        // save person
        return this.save(person);
    }
    
}
